<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商名称" prop="supplierName" label-width="100px">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="proName" label-width="100px">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入产品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:supplierProduct:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:supplierProduct:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column label="供应商" align="center" prop="supplierName" >
        <template slot-scope="scope">
          <el-tag type="primary" size="small">{{ scope.row.supplierName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品" align="center" prop="proName" >
        <template slot-scope="scope">
          <el-tag type="success" size="small">{{ scope.row.proName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="日产量(kg)" align="center" prop="dailyProduction" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:supplierProduct:edit']"
          >修改产能</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:supplierProduct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加产品和其对应的日产量 -->
    <el-dialog :title="title" :visible.sync="openAdd" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品" prop="proId">
          <treeselect v-model="form.proId" :options="proOptions" :props="{label: 'proName', children: 'children'}" placeholder="请选择产品" :normalizer="normalizer" />
        </el-form-item>
        <el-form-item label="日产量" prop="dailyProduction">
          <el-input v-model="form.dailyProduction" placeholder="请输入日产量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改产品对应的日产量 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日产量(kg)" prop="dailyProduction" label-width="100px">
          <el-input v-model="form.dailyProduction" placeholder="请输入日产量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSupplierProduct, getSupplierProduct, delSupplierProduct, addSupplierProduct, updateSupplierProduct } from "@/api/system/supplierProduct";
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import Treeselect from "@riophae/vue-treeselect";
import {listProduct} from "@/api/system/product";

export default {
  name: "SupplierProduct",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商对应经营的产品表格数据
      productList: [],

      proOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openAdd: false,
      openEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierId: null,
        supplierName: null,
        proId: null,
        proName: null,
        dailyProduction: null
      },
      // 表单参数
      form: {
      },

      // 表单校验
      rules: {
        supplierId: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        proId: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        dailyProduction: [
          { required: true, message: "日产量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商对应经营的产品列表 */
    getList() {
      this.loading = true;
      listSupplierProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openAdd = false;
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        proId: null,
        dailyProduction: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supplierId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openAdd = true;
      this.title = "添加供应商对应经营的产品";
      listProduct().then(response => {
        this.proOptions = this.handleTree(response.data, "proId", "proParentId");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids
      getSupplierProduct(supplierId).then(response => {
        this.form = response.data;
        this.form.supplierId = supplierId;
        this.openEdit = true;
        this.title = "修改供应商对应经营的产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const payload = {
            ...this.form,
          };
          if (this.form.supplierId != null) {
            updateSupplierProduct(payload).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.getList();
            });
          } else {
            addSupplierProduct(payload).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openAdd = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierIds = row.supplierId || this.ids;
      this.$modal.confirm('是否确认删除供应商对应经营的产品编号为"' + supplierIds + '"的数据项？').then(function() {
        return delSupplierProduct(supplierIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.proId,
        label: node.proName,
        children: node.children
      };
    },
  }
};
</script>
